package com.cis.fingerprint.utils;

import org.springframework.util.ClassUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BmpUtils {

    public static final String imgPath = ClassUtils.getDefaultClassLoader().getResource("static/images").getPath()+"/fingerprint.bmp";

    /**
     * 保存8位位图，需要添加颜色调色板，文件信息头+位图信息头+调色板+位图数据
     * @param data 位图数据
     */
    public static void getBmpWith8(byte[] data){
        int w = 256;
        int h = 288;
        //颜色表
        byte[] colorTable = addBMP8ImageInfosHeaderTable(w,h);
        byte[] infos = addBMP8ImageInfosHeader(w,h);
        byte[] header = addBMP8ImageHeader(data.length, infos.length + colorTable.length);
        //用来组合上面四个部分的空间，这个空间直接保存就是位图了
        byte[] buffer = new byte[header.length + infos.length + colorTable.length + data.length];
        //复制文件头
        System.arraycopy(header, 0, buffer, 0, header.length);
        //复制文件信息头
        System.arraycopy(infos, 0, buffer, header.length, infos.length);
        //复制颜色表
        System.arraycopy(colorTable, 0, buffer, header.length + infos.length, colorTable.length);
        System.arraycopy(data, 0, buffer, header.length + infos.length + colorTable.length , data.length);
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(imgPath);
            fos.write(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }

    }

    private static byte[] addBMP8ImageHeader(int size, int length) {
        byte[] buffer = new byte[14];
        //文件信息头和颜色表长度之和
        int m_length = length + buffer.length;
        size += m_length;
        buffer[0] = 0x42;
        buffer[1] = 0x4D;
        buffer[2] = (byte) (size >> 0);
        buffer[3] = (byte) (size >> 8);
        buffer[4] = (byte) (size >> 16);
        buffer[5] = (byte) (size >> 24);
        buffer[6] = 0x00;
        buffer[7] = 0x00;
        buffer[8] = 0x00;
        buffer[9] = 0x00;
        buffer[10] = (byte) (m_length >> 0);
        buffer[11] = (byte) (m_length >> 8);
        buffer[12] = (byte) (m_length >> 16);
        buffer[13] = (byte) (m_length >> 24);
        return buffer;
    }

    private static byte[] addBMP8ImageInfosHeader(int w, int h) {
        byte[] buffer = new byte[40];
        int ll = buffer.length;
        buffer[0] = (byte) (ll >> 0);    //DWORD：本段头长度：40   0x0028
        buffer[1] = (byte) (ll >> 8);
        buffer[2] = (byte) (ll >> 16);
        buffer[3] = (byte) (ll >> 24);
        buffer[4] = (byte) (w >> 0);    //long：图片宽度
        buffer[5] = (byte) (w >> 8);
        buffer[6] = (byte) (w >> 16);
        buffer[7] = (byte) (w >> 24);
        buffer[8] = (byte) (h >> 0);    //long：图片高度
        buffer[9] = (byte) (h >> 8);
        buffer[10] = (byte) (h >> 16);
        buffer[11] = (byte) (h >> 24);
        buffer[12] = 0x01;           //WORD:平面数：1
        buffer[13] = 0x00;
        buffer[14] = 0x08;           //WORD:图像位数：8位
        buffer[15] = 0x00;
        buffer[16] = 0x00;           //DWORD：压缩方式，可以是0，1，2，
        buffer[17] = 0x00;           //其中0表示不压缩
        buffer[18] = 0x00;
        buffer[19] = 0x00;
        buffer[20] = 0x00;           //DWORD；实际位图数据占用的字节数,当上一个数值
        buffer[21] = 0x00;           //biCompression等于0时，这里的值可以省略不填
        buffer[22] = 0x00;
        buffer[23] = 0x00;
        buffer[24] = (byte) 0x20;    //LONG：X方向分辨率
        buffer[25] = 0x4E;           //20000(0x4E20) dpm  1 in = 0.0254 m
        buffer[26] = 0x00;
        buffer[27] = 0x00;
        buffer[28] = (byte) 0x20;    //LONG：Y方向分辨率
        buffer[29] = 0x4E;           //20000(0x4E20) dpm  1 in = 0.0254 m
        buffer[30] = 0x00;
        buffer[31] = 0x00;
        buffer[32] = 0x00;           //DWORD：使用的颜色数，如果为0，
        buffer[33] = 0x00;           //则表示默认值(2^颜色位数)
        buffer[34] = 0x00;
        buffer[35] = 0x00;
        buffer[36] = 0x00;           //DWORD：重要颜色数，如果为0,
        buffer[37] = 0x00;           //则表示所有颜色都是重要的
        buffer[38] = 0x00;
        buffer[39] = 0x00;

        return buffer;
    }

    private static byte[] addBMP8ImageInfosHeaderTable(int w, int h) {
        byte[] buffer = new byte[256 * 4];

        //生成颜色表
        for (int i = 0; i < 256; i++) {
            buffer[0 + 4 * i] = (byte) i;   //Blue
            buffer[1 + 4 * i] = (byte) i;   //Green
            buffer[2 + 4 * i] = (byte) i;   //Red
            buffer[3 + 4 * i] = (byte) 0x00;   //保留值
        }

        return buffer;
    }
}
